﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YArchitech.Controls;
using YJKAreaStatistic.DataDefine;
using YJKRevitBase.StringUtility;

namespace YJKAreaStatistic.Manage
{
	internal class InsertDataManager
	{
		public static void WriteExtensibleStorage(Autodesk.Revit.DB.Document doc, IDictionary<int, List<string>> data)
		{
			Entity entity = null;
			IDictionary<int, string> dictionary = new Dictionary<int, string>();
			IDictionary<int, string> dictionary2 = new Dictionary<int, string>();
			if (doc.ProjectInformation == null)
			{
				YJKMessageBox.Show("项目工程信息损坏，请重启软件!");
			}
			foreach (KeyValuePair<int, List<string>> keyValuePair in data)
			{
				string value = StrEscapeTool.EscapeAndMerge(keyValuePair.Value);
				dictionary.Add(keyValuePair.Key, value);
			}
			Guid guid = new Guid(InsertDataManager.m_strAddDataGUID);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(InsertDataManager.m_strAddColSchemaName);
				schemaBuilder.AddMapField(InsertDataManager.m_strAddColFieldName, typeof(int), typeof(string));
				schema = schemaBuilder.Finish();
			}
			else
			{
				try
				{
					entity = doc.ProjectInformation.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						dictionary = entity.Get<IDictionary<int, string>>(InsertDataManager.m_strAddColFieldName);
					}
					foreach (KeyValuePair<int, List<string>> keyValuePair2 in data)
					{
						if (dictionary.ContainsKey(keyValuePair2.Key))
						{
							dictionary2.Add(keyValuePair2.Key, dictionary[keyValuePair2.Key]);
						}
						else
						{
							string value2 = StrEscapeTool.EscapeAndMerge(keyValuePair2.Value);
							dictionary2.Add(keyValuePair2.Key, value2);
						}
					}
					Field field = schema.GetField(InsertDataManager.m_strAddColFieldName);
					entity.Set<IDictionary<int, string>>(field, dictionary2);
					doc.ProjectInformation.SetEntity(entity);
					return;
				}
				catch (Exception)
				{
					return;
				}
			}
			Entity entity2 = new Entity(schema);
			Field field2 = schema.GetField(InsertDataManager.m_strAddColFieldName);
			entity2.Set<IDictionary<int, string>>(field2, dictionary);
			doc.ProjectInformation.SetEntity(entity2);
		}

		public static void WriteFormRecordData(Autodesk.Revit.DB.Document doc, List<List<string>> data)
		{
			List<string> list = new List<string>();
			if (doc.ProjectInformation == null)
			{
				YJKMessageBox.Show("项目工程信息损坏，请重启软件!");
			}
			foreach (List<string> input in data)
			{
				string item = StrEscapeTool.EscapeAndMerge(input);
				list.Add(item);
			}
			Guid guid = new Guid(InsertDataManager.m_strFormRecordDataGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(InsertDataManager.m_strFormRecordDataSchemaName);
				schemaBuilder.AddArrayField(InsertDataManager.m_strFormRecordDataFileName, typeof(string));
				schema = schemaBuilder.Finish();
			}
			else
			{
				try
				{
					Entity entity = doc.ProjectInformation.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						list.AddRange(entity.Get<IList<string>>(InsertDataManager.m_strFormRecordDataFileName));
					}
					else if (entity != null && entity.Schema == null)
					{
						doc.ProjectInformation.DeleteEntity(schema);
						Entity entity2 = new Entity(schema);
						Field field = schema.GetField(InsertDataManager.m_strFormRecordDataFileName);
						entity2.Set<IList<string>>(field, list);
						doc.ProjectInformation.SetEntity(entity2);
						return;
					}
					Field field2 = schema.GetField(InsertDataManager.m_strFormRecordDataFileName);
					entity.Set<IList<string>>(field2, list);
					doc.ProjectInformation.SetEntity(entity);
					return;
				}
				catch (Exception)
				{
					return;
				}
			}
			Entity entity3 = new Entity(schema);
			Field field3 = schema.GetField(InsertDataManager.m_strFormRecordDataFileName);
			entity3.Set<IList<string>>(field3, list);
			doc.ProjectInformation.SetEntity(entity3);
		}

		public static IDictionary<int, string> ReadExtensibleStorage(Autodesk.Revit.DB.Document doc)
		{
			Schema schema = Schema.Lookup(new Guid(InsertDataManager.m_strAddDataGUID));
			if (schema == null)
			{
				return null;
			}
			if (doc.ProjectInformation == null)
			{
				YJKMessageBox.Show("项目工程信息损坏，请重启软件!");
			}
			Entity entity = doc.ProjectInformation.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return null;
			}
			return entity.Get<IDictionary<int, string>>(InsertDataManager.m_strAddColFieldName);
		}

		public static bool ReadFormRecordData(Autodesk.Revit.DB.Document doc, ref List<RecordCellData> lstRecordFormData)
		{
			Schema schema = Schema.Lookup(new Guid(InsertDataManager.m_strFormRecordDataGuid));
			if (schema == null)
			{
				return false;
			}
			if (doc.ProjectInformation == null)
			{
				YJKMessageBox.Show("项目工程信息损坏，请重启软件!");
			}
			Entity entity = doc.ProjectInformation.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return false;
			}
			try
			{
				foreach (string input in entity.Get<IList<string>>(InsertDataManager.m_strFormRecordDataFileName).ToList<string>())
				{
					List<string> list = StrEscapeTool.UnEsapeAneUnMerge(input);
					if (list.Count == 3)
					{
						lstRecordFormData.Add(new RecordCellData(list[0], list[1], list[2]));
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static bool ReadExtensibleStorageFromArea(Area area, ref List<ElementId> roomIdList)
		{
			Schema schema = Schema.Lookup(new Guid(InsertDataManager.m_strAreaInfoGuid));
			if (schema == null)
			{
				return false;
			}
			Entity entity = area.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			if (entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField(InsertDataManager.m_strRoomIdListFieldName);
			foreach (int num in entity.Get<IList<int>>(field))
			{
				roomIdList.Add(new ElementId(num));
			}
			return true;
		}

		public static void WriteExtensibleStorageToArea(Area area, AreaType areaType, string suiteName, string hoomNumb, List<ElementId> roomIdList)
		{
			Guid guid = new Guid(InsertDataManager.m_strAreaInfoGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(InsertDataManager.m_strSchemaName);
				schemaBuilder.AddSimpleField(InsertDataManager.m_strAreaTypeFieldName, typeof(int));
				schemaBuilder.AddSimpleField(InsertDataManager.m_strSuiteNameFieldName, typeof(string));
				schemaBuilder.AddSimpleField(InsertDataManager.m_strHoomNumbFieldName, typeof(string));
				schemaBuilder.AddArrayField(InsertDataManager.m_strRoomIdListFieldName, typeof(int));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField(InsertDataManager.m_strAreaTypeFieldName);
			entity.Set<int>(field, (int)areaType);
			Field field2 = schema.GetField(InsertDataManager.m_strSuiteNameFieldName);
			entity.Set<string>(field2, suiteName);
			Field field3 = schema.GetField(InsertDataManager.m_strHoomNumbFieldName);
			entity.Set<string>(field3, hoomNumb);
			Field field4 = schema.GetField(InsertDataManager.m_strRoomIdListFieldName);
			IList<int> list = new List<int>();
			foreach (ElementId elementId in roomIdList)
			{
				list.Add(elementId.IntegerValue);
			}
			entity.Set<IList<int>>(field4, list);
			area.SetEntity(entity);
		}

		public static void WriteExensibleStorageToCurve(Element dcv, bool bIsDraw)
		{
			Guid guid = new Guid(InsertDataManager.m_strAddInfoToCurveGUID);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName(InsertDataManager.m_strCurveInfoSchemaName);
				schemaBuilder.AddSimpleField(InsertDataManager.m_strCurveInfoFileName, typeof(bool));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField(InsertDataManager.m_strCurveInfoFileName);
			entity.Set<bool>(field, bIsDraw);
			dcv.SetEntity(entity);
		}

		public static bool ReadExensibleStorageToCurve(DetailCurve dcv)
		{
			Schema schema = Schema.Lookup(new Guid(InsertDataManager.m_strAddInfoToCurveGUID));
			if (schema == null)
			{
				return false;
			}
			Entity entity = dcv.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			if (entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField(InsertDataManager.m_strCurveInfoFileName);
			return entity.Get<bool>(field);
		}

		public static List<ElementId> ReadArchAreaData(Element element)
		{
			try
			{
				Schema schema = Schema.Lookup(new Guid(InsertDataManager.m_ArchGuid));
				if (schema != null)
				{
					Entity entity = element.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						Field field = schema.GetField("Boundary");
						return entity.Get<IList<ElementId>>(field).ToList<ElementId>();
					}
				}
			}
			catch
			{
			}
			return null;
		}

		public static List<ElementId> ReadInnerAreaData(Element element)
		{
			try
			{
				Schema schema = Schema.Lookup(new Guid(InsertDataManager.m_InnerGuid));
				if (schema != null)
				{
					Entity entity = element.GetEntity(schema);
					if (entity != null && entity.Schema != null)
					{
						Field field = schema.GetField("Boundary");
						return entity.Get<IList<ElementId>>(field).ToList<ElementId>();
					}
				}
			}
			catch
			{
			}
			return null;
		}

		private static string m_strAddDataGUID = "4D628B43-08CE-4AB6-B4D5-E9C764DCAC3D";

		private static string m_strAddColSchemaName = "addColExtensibleInfo";

		private static string m_strAddColFieldName = "addColDatas";

		private static string m_strFormRecordDataGuid = "1A098B30-393D-4B5F-8382-22BEFCCABFC1";

		private static string m_strFormRecordDataSchemaName = "RecordDataExtensibleInfo";

		private static string m_strFormRecordDataFileName = "RecordData";

		private static string m_strAreaInfoGuid = "82FEEA0A-95F2-4D07-AB7C-73E4EFFDA615";

		private static string m_strRoomIdListFieldName = "RoomIdList";

		private static string m_strSchemaName = "AreaExtensibleInfo";

		private static string m_strAreaTypeFieldName = "AreaType";

		private static string m_strSuiteNameFieldName = "SuiteName";

		private static string m_strHoomNumbFieldName = "HoomNumb";

		private static string m_strAddInfoToCurveGUID = "719ABADB-E42E-43F8-8B64-3615429A2B97";

		private static string m_strCurveInfoSchemaName = "CurveExtensibleInfo";

		private static string m_strCurveInfoFileName = "IsDrawCurve";

		private static List<string> lstFileNames = new List<string>();

		private static string m_ArchGuid = "772B74F8-FB48-43C7-9E4F-B17036EF2895";

		private static string m_InnerGuid = "A07967B8-1C19-4700-BC62-A4C55CE08717";
	}
}
